import { PasterGateway, Paster, PasterInterfaceWithDerive, PageData, DisableRes, PutRes, putPasterDto, disablePasterDto } from "qqlx-core";

import { ViewBase, ViewBaseAction } from "@view/view-base";
import { PasterSchema } from "@/pond/schema-asset";

export class ViewPaster extends ViewBase<Paster> implements ViewBaseAction<Paster>, PasterInterfaceWithDerive<{}> {
  private static instance: ViewPaster;
  static getInstance(options?: { isSingleton?: boolean }) {
    if (options?.isSingleton === false) {
      return new ViewPaster();
    }

    if (!this.instance) this.instance = new ViewPaster();
    return this.instance;
  }

  private constructor() {
    super();
    this.initial();
    this.insertDefaultCondition();
  }

  getSchema(): Paster {
    return new PasterSchema();
  }

  async get() {
    this.pending();
    const { list, total } = await this.$request.getByPut<null, PageData<Paster>>(PasterGateway.path);
    this.data.entityListTotal = total;
    return { list, total };
  }

  async put() {
    this.pending();
    const dto = { schema: this.data.schema };
    return this.$request.put<putPasterDto, PutRes<Paster>>(PasterGateway.path, dto);
  }

  async disable(dto: disablePasterDto) {
    this.pending();
    return this.$request.delete<disablePasterDto, DisableRes>(PasterGateway.path, dto);
  }
}
